package uit.ce03.game_ninja.MainGame;


import uit.ce03.game_ninja.NinjaActivity;
import uit.ce03.game_ninja.R;
import uit.ce03.game_ninja.Static.Static_Const;
import uit.ce03.game_ninja.database.Database;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Luudiem extends Activity {
	
	Button menu, choilai, luu;
	TextView text_diem;
	EditText edit_ten;
	private Database db;
	MediaPlayer mp;
	
	int diem = 0;
	// ------------------------------------------------------------------------------
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.luudiem);
		
		 mp = MediaPlayer.create(this, R.raw.nhac_nen_game_1);
	        if(Static_Const.AmThanh)
	        {
	        	mp.start();
	        	mp.setLooping(true);
	        }
		
		db = new Database(this);
		
		Bundle b = getIntent().getExtras();
		
		if(b != null){
			diem = b.getInt("diem");
		}

		choilai = (Button) findViewById(R.id.choilai);	
		luu = (Button) findViewById(R.id.luu);
		menu = (Button) findViewById(R.id.menu);
		text_diem = (TextView) findViewById(R.id.diem);
		text_diem.setText(String.valueOf(diem));
		edit_ten = (EditText) findViewById(R.id.name);
		
		choilai.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(Luudiem.this, MainGame_BaseGame.class);
				startActivity(i);
				mp.stop();
				finish();
			}
		});
		luu.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				luu();
				
			}
		});
		menu.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(Luudiem.this, NinjaActivity.class);
				startActivity(i);
				mp.stop();
				finish();
			}
		});
	}

	// ------------------------------------------------------------------------------
	private void luu() {
		String name = edit_ten.getText().toString();
		int diemchoi = Integer.parseInt(text_diem.getText().toString());
		if (name.length() == 0 || name == null) {
			Toast.makeText(this, "Bạn Chưa nhập Tên !!", Toast.LENGTH_LONG).show();
			return;
		}
		try {
			db.open();
			Cursor c = db.getAllRows();
			if (c.moveToPosition(9)) {
					String id = c.getString(c.getColumnIndex(Database.KEY_ROWID));
					int math = c.getInt(c.getColumnIndex(Database.KEY_DIEM));
						if (math < diemchoi) {
							db.updatePlayer(id, name, diemchoi);
							LinearLayout l = (LinearLayout)findViewById(R.id.linearLayout_diem);
							l.setVisibility(View.GONE);
							luu.setVisibility(View.GONE);
						}

			}
			c.close();
			db.close();
		} catch (SQLiteException e) {
			e.printStackTrace();
		}
	}

	// ------------------------------------------------------------------------------
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		this.finish();
		super.onPause();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		this.finish();
	};

	// ------------------------------------------------------------------------------
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			AlertDialog.Builder alertDialog = new AlertDialog.Builder(Luudiem.this);
			alertDialog.setTitle("Exit");
			alertDialog.setMessage("Bạn Có Muốn Thoát Ra khỏi Game Không ?");
			alertDialog.setIcon(R.drawable.icon_exit);
			alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					mp.stop();
					finish();
				}
			});
			alertDialog.setNegativeButton("NO",new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					dialog.cancel();
				}
			});
			alertDialog.show();
		}
		return super.onKeyDown(keyCode, event);
	}

	// ------------------------------------------------------------------------------
	
}
